{% extends "admin/base_site.html" %}
{% load static %}

{% block title %}综合仪表板 - {{ site_title|default:"Django 管理" }}{% endblock %}

{% block extrahead %}
<style>
.dashboard-container {
    padding: 20px;
    background: #f8f9fa;
    min-height: calc(100vh - 120px);
}

.dashboard-header {
    background: linear-gradient(135deg, #007cba 0%, #0056b3 100%);
    color: white;
    padding: 30px;
    border-radius: 12px;
    margin-bottom: 30px;
    box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}

.dashboard-header h1 {
    margin: 0;
    font-size: 2.5em;
    font-weight: 300;
}

.dashboard-header p {
    margin: 10px 0 0 0;
    opacity: 0.9;
    font-size: 1.1em;
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 20px;
    margin-bottom: 30px;
}

.stat-card {
    background: white;
    padding: 25px;
    border-radius: 12px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    border-left: 4px solid #007cba;
    transition: transform 0.2s, box-shadow 0.2s;
}

.stat-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0,0,0,0.15);
}

.stat-card h3 {
    margin: 0 0 10px 0;
    color: #495057;
    font-size: 0.9em;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.stat-card .value {
    font-size: 2.2em;
    font-weight: bold;
    color: #007cba;
    margin: 0;
}

.stat-card .change {
    font-size: 0.85em;
    margin-top: 5px;
}

.change.positive {
    color: #28a745;
}

.change.negative {
    color: #dc3545;
}

.content-grid {
    display: grid;
    grid-template-columns: 2fr 1fr;
    gap: 30px;
    margin-bottom: 30px;
}

.chart-container {
    background: white;
    padding: 25px;
    border-radius: 12px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.chart-container h2 {
    margin: 0 0 20px 0;
    color: #495057;
    font-size: 1.3em;
}

.recent-battles {
    background: white;
    padding: 25px;
    border-radius: 12px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.recent-battles h2 {
    margin: 0 0 20px 0;
    color: #495057;
    font-size: 1.3em;
}

.battle-item {
    padding: 15px;
    border-bottom: 1px solid #e9ecef;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.battle-item:last-child {
    border-bottom: none;
}

.battle-players {
    font-weight: 500;
}

.battle-result {
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 0.8em;
    font-weight: bold;
}

.result-win {
    background: #d4edda;
    color: #155724;
}

.result-loss {
    background: #f8d7da;
    color: #721c24;
}

.result-draw {
    background: #fff3cd;
    color: #856404;
}

.result-error {
    background: #f5c6cb;
    color: #721c24;
}

.top-players {
    background: white;
    padding: 25px;
    border-radius: 12px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    margin-bottom: 30px;
}

.top-players h2 {
    margin: 0 0 20px 0;
    color: #495057;
    font-size: 1.3em;
}

.player-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 0;
    border-bottom: 1px solid #e9ecef;
}

.player-item:last-child {
    border-bottom: none;
}

.player-rank {
    font-weight: bold;
    color: #007cba;
    margin-right: 15px;
}

.player-stats {
    font-size: 0.9em;
    color: #6c757d;
}

.progress-bar {
    width: 100%;
    height: 8px;
    background: #e9ecef;
    border-radius: 4px;
    overflow: hidden;
    margin: 10px 0;
}

.progress-fill {
    height: 100%;
    background: linear-gradient(90deg, #28a745, #20c997);
    transition: width 0.3s ease;
}

.language-stats {
    background: white;
    padding: 25px;
    border-radius: 12px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.language-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 0;
}

.language-name {
    font-weight: 500;
}

.language-count {
    background: #007cba;
    color: white;
    padding: 2px 8px;
    border-radius: 12px;
    font-size: 0.8em;
}
</style>
{% endblock %}

{% block content %}
<div class="dashboard-container">
    <div class="dashboard-header">
        <h1>📊 综合仪表板</h1>
        <p>实时监控系统状态和用户活动</p>
    </div>

    <!-- 统计卡片 -->
    <div class="stats-grid">
        <div class="stat-card">
            <h3>总用户数</h3>
            <div class="value">{{ total_users }}</div>
            <div class="change positive">+{{ active_users_7d }} 活跃用户（7天）</div>
        </div>
        
        <div class="stat-card">
            <h3>总对战数</h3>
            <div class="value">{{ total_battles }}</div>
            <div class="change positive">+{{ recent_battles_count }} 最近7天</div>
        </div>
        
        <div class="stat-card">
            <h3>代码总数</h3>
            <div class="value">{{ total_codes }}</div>
        </div>
        
        <div class="stat-card">
            <h3>进行中对战</h3>
            <div class="value">{{ active_battles }}</div>
        </div>
        
        <div class="stat-card">
            <h3>平均对战时长</h3>
            <div class="value">{{ recent_avg_duration }}</div>
        </div>
        
        <div class="stat-card">
            <h3>错误率</h3>
            <div class="value">{{ error_rate }}</div>
        </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="content-grid">
        <!-- 对战结果分布图表 -->
        <div class="chart-container">
            <h2>📈 对战结果分布</h2>
            <div style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 20px;">
                <div>
                    <div style="display: flex; justify-content: space-between; margin-bottom: 10px;">
                        <span>玩家1获胜</span>
                        <strong>{{ battle_results.player1_win }}</strong>
                    </div>
                    <div class="progress-bar">
                        <div class="progress-fill" style="width: {% widthratio battle_results.player1_win total_battles 100 %}%; background: #28a745;"></div>
                    </div>
                </div>
                
                <div>
                    <div style="display: flex; justify-content: space-between; margin-bottom: 10px;">
                        <span>玩家2获胜</span>
                        <strong>{{ battle_results.player2_win }}</strong>
                    </div>
                    <div class="progress-bar">
                        <div class="progress-fill" style="width: {% widthratio battle_results.player2_win total_battles 100 %}%; background: #007cba;"></div>
                    </div>
                </div>
                
                <div>
                    <div style="display: flex; justify-content: space-between; margin-bottom: 10px;">
                        <span>平局</span>
                        <strong>{{ battle_results.draw }}</strong>
                    </div>
                    <div class="progress-bar">
                        <div class="progress-fill" style="width: {% widthratio battle_results.draw total_battles 100 %}%; background: #ffc107;"></div>
                    </div>
                </div>
                
                <div>
                    <div style="display: flex; justify-content: space-between; margin-bottom: 10px;">
                        <span>错误</span>
                        <strong>{{ battle_results.error }}</strong>
                    </div>
                    <div class="progress-bar">
                        <div class="progress-fill" style="width: {% widthratio battle_results.error total_battles 100 %}%; background: #dc3545;"></div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 最近对战 -->
        <div class="recent-battles">
            <h2>🕒 最近对战</h2>
            {% for battle in recent_battles %}
            <div class="battle-item">
                <div>
                    <div class="battle-players">
                        {{ battle.player1.username }} vs {{ battle.player2.username }}
                    </div>
                    <div style="font-size: 0.8em; color: #6c757d;">
                        {{ battle.created_at|date:"m-d H:i" }}
                    </div>
                </div>
                <div>
                    {% if battle.result == 'player1_win' %}
                        <span class="battle-result result-win">P1胜</span>
                    {% elif battle.result == 'player2_win' %}
                        <span class="battle-result result-win">P2胜</span>
                    {% elif battle.result == 'draw' %}
                        <span class="battle-result result-draw">平局</span>
                    {% elif battle.result == 'error' %}
                        <span class="battle-result result-error">错误</span>
                    {% else %}
                        <span class="battle-result" style="background: #e9ecef; color: #495057;">进行中</span>
                    {% endif %}
                </div>
            </div>
            {% endfor %}
        </div>
    </div>

    <!-- 顶级玩家和语言统计 -->
    <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 30px;">
        <!-- 顶级玩家 -->
        <div class="top-players">
            <h2>🏆 顶级玩家</h2>
            {% for player in top_players %}
            <div class="player-item">
                <div style="display: flex; align-items: center;">
                    <span class="player-rank">#{{ forloop.counter }}</span>
                    <div>
                        <div style="font-weight: 500;">{{ player.user.username }}</div>
                        <div class="player-stats">
                            胜率: {{ player.stats.win_rate|floatformat:1 }}% | 
                            对战: {{ player.stats.total_battles }}场
                        </div>
                    </div>
                </div>
                <div style="text-align: right;">
                    <div style="font-weight: bold; color: #007cba;">
                        {{ player.score|floatformat:2 }}
                    </div>
                </div>
            </div>
            {% endfor %}
        </div>

        <!-- 编程语言统计 -->
        <div class="language-stats">
            <h2>💻 编程语言统计</h2>
            {% for lang in language_stats.code_languages %}
            <div class="language-item">
                <span class="language-name">{{ lang.language|default:"未知" }}</span>
                <span class="language-count">{{ lang.count }}</span>
            </div>
            {% endfor %}
        </div>
    </div>
</div>
{% endblock %}